<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

/**
 * App\Models\Appraise
 *
 * @property int $id
 * @property int $user_id 用户ID
 * @property int $type 类型:0=评价洗车服务,1=评价商品订单
 * @property int $order_id 订单ID
 * @property int $detail_id 订单详情
 * @property int $good_id 商品ID
 * @property int $service_id 服务ID
 * @property int $worker_id 师傅ID
 * @property int $shop_id 店铺ID
 * @property int $score 评分
 * @property string $content 评价内容
 * @property mixed|null $images 评价图片
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|Appraise newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Appraise newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Appraise query()
 * @method static \Illuminate\Database\Eloquent\Builder|Appraise whereContent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Appraise whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Appraise whereDetailId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Appraise whereGoodId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Appraise whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Appraise whereImages($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Appraise whereOrderNo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Appraise whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Appraise whereUserId($value)
 * @mixin \Eloquent
 * @property-read \App\Models\User|null $user
 * @method static \Illuminate\Database\Eloquent\Builder|Appraise whereOrderId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Appraise whereServiceId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Appraise whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Appraise whereScore($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Appraise whereShopId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Appraise whereWorkerId($value)
 */
class Appraise extends Model
{
	use HasDateTimeFormatter;

    protected $fillable = [
        'user_id','detail_id','type','order_id','good_id','service_id','shop_id','worker_id','score','content','images'
    ];

    protected $casts = [
        'images'=>'json'
    ];

    protected static function booted()
    {
        self::created(function (Appraise $row){

            if ($row->type){
//                $count1 = OrderDetail::whereOrderId($row->order_id)->count();
            }else{
                ServiceOrder::whereId($row->detail_id)->update(['status'=>3]);
            }
        });
    }

    /**
     * @param $params
     * @return bool
     */
    public static function addApprise($params): bool
    {
        $detail_id = $params['detail_id'];
//        if ($params['type']==1){
////            $detail = OrderDetail::whereId($detail_id)->first();
////            $params['good_id'] = $detail->good_id;
//        }else{
//
//        }
        $detail = ServiceOrder::whereId($detail_id)->first();
        if (!$detail->state){
            modelError("请先确认完成订单!");
        }
        $params['service_id'] = $detail->service_id;
        $params['worker_id'] = $detail->worker_id;
        $params['shop_id'] = $detail->shop_id;
        $params['order_id'] = $detail->order_id;
        return (bool)self::create($params);
    }

    public function user(){
        return $this->hasOne(User::class,"id","user_id")->select(["id","name","nickname","avatar"]);
    }

    public static function getList($type){
        $limit = request()->input('limit',10);
        if ($type){
            $good_id = request()->input('good_id',0);
            $list = self::query()
                ->with('user')
                ->where("type",1)
                ->where("good_id",$good_id)
                ->paginate($limit);
        }else{
            $service_id = request()->input('service_id',0);
            $list = self::query()
                ->with('user')
                ->where("type",0)
                ->where("service_id",$service_id)
                ->paginate($limit);
        }
        foreach ($list as $item){
            if ($item->user){
                if ($item->user->avatar){
                    $item->user->avatar = cdn_url($item->user->avatar);
                }else{
                    $item->user->avatar = letter_avatar($item->user->name);
                }
            }
            $item->images = cdn_url($item->images);
            $item->time_text =  Carbon::parse($item->created_at)->diffForHumans();
        }
        $data['items'] = $list->items();
        $data['lastPage'] = $list->lastPage();
        $data['total'] = $list->total();
        return $data;
    }

}
